﻿using System.Linq;
using System.Deployment;
using System.Collections.Generic;
using System.Threading;
using w = Microsoft.Office.Interop.Word;
using System;
namespace z.m {
	partial class Order_Detail {
		partial void OnPriceChanged() {
			SendPropertyChanged("Total");
			if (Order == null) return;
			this.Order.refreshSumP();
		}
		partial void OnQuantityChanged() {
			SendPropertyChanged("Total");
			if (Order == null) return;
			this.Order.refreshSumP();
		}
		partial void OnCreated() {
			if (Quantity == 0) Quantity = 1;
		}
		partial void OnIDChanged() {
			if (Order != null)
				this.Order.refreshSumP();
		}
		public long Total {
			get {
				return Price * Quantity;
			}
		}
	}
	partial class Kind : IText {
		public static IQueryable<m.Kind> Search(string str) {
			return App.DB.Kinds.Where(x => x.Name.Contains(str))
				.Take(Properties.Settings.Default.autMaxSearch)
				.OrderBy(x=>x.Name);
		}
		public string FullText {
			get { return Name; }
		}
		public override string ToString() {
			return Name;
		}
	}
	partial class Order {
		public void refreshSumP() {
			SendPropertyChanged("Sum");
		}
		public void Report(bool isPrint,int type = 2) {
			string file = "Temp/";
			string strformat = "#,##0.000";
			//string file="~/Dat/";
			switch (type) {
				case 1: file += "Bien Nhan"; break;
				case 2: file += "HD Ban Le"; break;
				case 3: file += "Hoa Don"; break;
				case 4: file += "Bao Gia"; break;
				case 5: file += "Hoa Don Vat"; break;
			}
			file += ".doc";
			System.IO.FileInfo fi = new System.IO.FileInfo(file);
			w.Application A;
			if (isPrint) {
				A = App.W;
			} else {
				A = new w.Application();
			}
			w.Document doc = A.Documents.Add(Template: fi.FullName);
			Dictionary<string, string> dicVar = new Dictionary<string, string>();
			int[] ai=new int[0];
			switch (type) {
				case 1: ai = new int[] { 1, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0 }; break;
				case 2: ai = new int[] { 1, 6, 2, 0, 0, 0, 3, 4, 5, 0, 0 }; break;
				case 3: ai = new int[] { 1, 8, 0, 4, 3, 2, 5, 6, 7, 0, 0 }; break;
				case 4: ai = new int[] { 1, 6, 0, 4, 3, 2, 5, 0, 0, 0, 0 }; break;
				case 5: ai = new int[] { 1, 8, 0, 4, 3, 2, 5, 0, 0, 6, 7 }; break;
			}
			int xi = 1;
			
			w.Row endRow = doc.Tables[1].Rows[2];
			//STT,NOTE,P.Name,P.Code,P.Brand,P.Kind,Quantity,Price,Total,ii,TotalVat
			double SumVat=0;
			foreach (var x in Order_Details) {
				w.Row r;
				if (xi == Order_Details.Count)
					r = doc.Tables[1].Rows[xi + 1];
				else
					r = doc.Tables[1].Rows.Add(BeforeRow: endRow);
				if (ai[0] != 0) r.Cells[ai[0]].Range.Text = (xi++).ToString();
				if (ai[1] != 0) r.Cells[ai[1]].Range.Text = x.Note;
				if (ai[2] != 0) r.Cells[ai[2]].Range.Text = x.Product == null ? "" : x.Product.Name;
				if (ai[3] != 0) r.Cells[ai[3]].Range.Text = x.Product == null ? "" : x.Product.Code;
				if (ai[4] != 0) r.Cells[ai[4]].Range.Text = x.Product == null ? "" : (x.Product.Brand == null ? "" : x.Product.Brand.Name);
				if (ai[5] != 0) r.Cells[ai[5]].Range.Text = x.Product == null ? "" : (x.Product.Kind == null ? "" : x.Product.Kind.Name);
				if (ai[6] != 0) r.Cells[ai[6]].Range.Text = x.Quantity.ToString();
				if (ai[7] != 0) r.Cells[ai[7]].Range.Text = x.Price.ToString(strformat);
				if (ai[8] != 0) r.Cells[ai[8]].Range.Text = x.Total.ToString(strformat);
				if (ai[9] != 0) {
					double ii = Math.Floor(x.Price*1000 / 1.1);
					ii /= 1000;
					r.Cells[ai[9]].Range.Text = ii.ToString(strformat);
					ii *= x.Quantity;
					r.Cells[ai[10]].Range.Text = ii.ToString(strformat);
					SumVat += ii;
				}
				if (type == 1||type==129)
					r.Cells[2].Range.Text = v.ZCommand.note2string(x);
			}
			dicVar.Add("p.Name", Product!=null ? Product.Kind +" "+Product.Brand+" "+ Product.Code : " ");
			dicVar.Add("c.Name", Customer != null ? Customer.Name : " ");
			dicVar.Add("c.Phone", Customer != null ? Customer.Phone!=null?Customer.Phone: " " : " ");
			dicVar.Add("e.Name", Employee_N != null ? Employee_N.Name : " ");
			dicVar.Add("el.Name", Employee_L != null ? Employee_L.Name : " ");
			dicVar.Add("ID", ID.ToString());
			dicVar.Add("Sum", Sum.Value.ToString(strformat));
			dicVar.Add("SumVat", SumVat.ToString(strformat));
			dicVar.Add("Vat", (((double)Sum.Value)-SumVat).ToString(strformat));
			dicVar.Add("Sum.Text", c.ZM.tienthanhchu((double)Sum.Value*1000));
			dicVar.Add("Date_N", Date_N.HasValue ? Date_N.Value.ToString("dd/MM/yyyy") : " ");
			dicVar.Add("Date_H", Date_H.HasValue ? Date_H.Value.ToString("dd/MM/yyyy") : " ");
			dicVar.Add("IsGiao", Date_G.HasValue ? "Đã Giao Hàng" : " ");
			dicVar.Add("IsThu", Date_T.HasValue ? "Đã Thu Tiền" : " ");
			dicVar.Add("IsChi", Order_Type>127 ? " (Chi)" : " ");
			foreach (var x in dicVar.Keys) {
				doc.Variables[x].Value = dicVar[x];
			}
			doc.Fields.Update();
			if (isPrint) {
				doc.PrintOut();
				doc.Close(SaveChanges:false);
			} else {
				A.Visible = true;
			}
		}
		public void viewFixDetails() {
			var f = new v.sub.OrderFixDetail();
			f.DataContext = this;
			f.ShowDialog();
		}
		public void viewSaleDetails() {
			var f = new v.sub.OrderSaleDetail();
			f.DataContext = this;
			f.ShowDialog();
		}
		public void viewBuyDetails() {
			var f = new v.sub.OrderBuyDetail();
			f.DataContext = this;
			f.ShowDialog();
		}
		public string FixInfor {
			get {
				return string.Join(", " ,this.Order_Details.Select(x=>x.Note));
			}
		}
		public string SaleInfor {
			get {
				return string.Join(", ", this.Order_Details.Select(x =>(x.Product==null?x.Note:x.Product.Name)+" x"+x.Quantity));
			}
		}
		public decimal? Sum {
			get {
				return Order_Details.Sum(x=>x.Total);
			}
		}
	}
	partial class Customer : IText {
		public static IQueryable<m.Customer> Search(string str) {
			return App.DB.Customers.Where(x => x.Nick.Contains(str)
					//|| x.Name.Contains(str)
					|| x.Phone.Contains(str)
				).Take(Properties.Settings.Default.autMaxSearch)
				.OrderBy(x => x.Nick).ThenBy(x=>x.Phone);
		}
		public string FullText {
			get { return Nick + ", " + Phone; }
		}
		public override string ToString() {
			return Nick;
		}
	}
	partial class Employee : IText {
		public static IQueryable<m.Employee> Search(string str) {
			return App.DB.Employees.Where(x => x.Nick.Contains(str)||x.Name.Contains(str))
				.Take(Properties.Settings.Default.autMaxSearch)
				.OrderBy(x=>x.Nick)
				.ThenBy(x=>x.Name);
		}
		public string FullText {
			get { return Nick; }
		}
		public override string ToString() {
			return Nick;
		}
	}
	partial class Product : IText {
		public int CurrentPriceIn {
			get {
				if (PriceIns.Count == 0) return 0;
				return PriceIns.Where(x=>x.Date==PriceIns.Max(y => y.Date)).Max(x=>x.Price);
			}
		}
		public static IQueryable<m.Product> Search(string str) {
			return App.DB.Products.Where(x=>x.Name.Contains(str)
				).Take(Properties.Settings.Default.autMaxSearch)
				.OrderBy(x => x.Name);
		}
		public string FullText {
			get { return Name; }
		}
		public override string ToString() {
			return Name;
		}
	}
	partial class Brand:IText {
		public static IQueryable<m.Brand> Search(string str) {
			return App.DB.Brands.Where(x =>x.Name.Contains(str))
				.Take(Properties.Settings.Default.autMaxSearch)
				.OrderBy(x => x.Name);
		}
		public string FullText {
			get { return Name; }
		}
		public override string ToString() {
			return Name;
		}
	}
	partial class Producter : IText {
		public static IQueryable<m.Producter> Search(string str) {
			return App.DB.Producters.Where(x => x.Name.Contains(str))
				.Take(Properties.Settings.Default.autMaxSearch)
				.OrderBy(x => x.Name);
		}
		public string FullText {
			get { return Name; }
		}
		public override string ToString() {
			return Name;
		}
	}
	public interface IText {
		string FullText { get; }
	}
}